<?php

class gameRules {

    protected $sRulesName;
    protected $sPlayerLabel;
    protected $iRulesType;
    protected $iMatchEndType;
    protected $iMatchEndValue;
    protected $iWinPoints;
    //game win
    static $GW_Less = 0;
    static $GW_Greater = 1;
    //end match
    static $EM_GAME = 0;
    static $EM_POINTS_DIF = 1;
    static $EM_POINTS = 2;
    static $EM_WINS = 3;
    //rules type
    static $RT_MATRIX = 0;
    static $RT_TREE = 1;
    //tree node
    static $TN_PLAYER1 = 0;
    static $TN_PLAYER2 = 1;
    static $TN_NATURE = 2;
    static $TN_POINTS = 3;
    static $aGroupColor = array(
        '#ffffff',
        '#3a6bff',
        '#53FF1A',
        '#FF531A',
        '#C61AFF',
        '#FFFF0A',
        '#FFD5FF',
        '#1AFFC6',
        '#996699',
        '#ff6600',
        '#5781FF',
        '#669900',
        '#cc6600',
        '#C6FF1A',
        '#FF1A53',
        '#CCCC00',
        '#4646cc',
        '#cc9933',
        '#6BB668',
        '#FF0A0A',
        '#FF60F0',
        '#795899',
        '#ff9900',
        '#ffcccc',
        '#8380f6',
        '#c3c673',
        '#997958',
        //powtorka
        '#3a6bff',
        '#53FF1A',
        '#FF531A',
        '#C61AFF',
        '#FFFF0A',
        '#FFD5FF',
        '#1AFFC6',
        '#996699',
        '#ff6600',
        '#5781FF',
        '#669900',
        '#cc6600',
        '#C6FF1A',
        '#FF1A53',
        '#CCCC00',
        '#4646cc',
        '#cc9933',
        '#6BB668',
        '#FF0A0A',
        '#FF60F0',
        '#795899',
        '#ff9900',
        '#ffcccc',
        '#8380f6',
        '#c3c673',
        '#997958');

    function __construct($iType, $sName, $sPlayer1, $sPlayer2) {
        $this->sRulesName = $sName;
        $this->sPlayerLabel[0] = $sPlayer1;
        $this->sPlayerLabel[1] = $sPlayer2;
        $this->iRulesType = $iType;
    }

    function getType() {
        return $this->iRulesType;
    }

    function getName() {
        return $this->sRulesName;
    }

    function getPlayerLabel($iIndex) {
        return $this->sPlayerLabel[$iIndex];
    }

    function getMatchEndType() {
        return $this->iMatchEndType;
    }

    function getMatchEndValue() {
        return $this->iMatchEndValue;
    }

    function getWinPoints() {
        return $this->iWinPoints;
    }

    static function getGroupColor($iIndex) {
        return isset(self::$aGroupColor[$iIndex])?self::$aGroupColor[$iIndex]:'#ffffff';
    }

}

class gameRulesMatrix extends gameRules {

    private $aRowsLabel;
    private $aColsLabel;
    private $aCellList;

    function __construct($sName, $sPlayer1, $sPlayer2, $aRow, $aCol, $aCell, $iMachEnd, $iMachEndV, $iWinPts) {
        $this->sRulesName = $sName;
        $this->sPlayerLabel = array($sPlayer1, $sPlayer2);
        $this->iRulesType = gameRules::$RT_MATRIX;
        $this->aRowsLabel = $aRow;
        $this->aColsLabel = $aCol;
        $this->aCellList = $aCell;
        $this->iMatchEndType = $iMachEnd;
        $this->iMatchEndValue = $iMachEndV;
        $this->iWinPoints = $iWinPts;
    }

    function getRow($iIntex) {
        return $this->aRowsLabel[$iIntex];
    }

    function getRows() {
        return $this->aRowsLabel;
    }

    function getCols() {
        return $this->aColsLabel;
    }

    function getCol($iIntex) {
        return $this->aColsLabel[$iIntex];
    }

    function getCell($iXIntdex, $iYIntdex) {
        return $this->aCellList[$iYIntdex * count($this->aRowsLabel) + $iXIntdex];
    }

    function getSize() {
        return array(count($this->aRowsLabel), count($this->aColsLabel));
    }

}

class gameRulesTree extends gameRules {

    private $aContent; //tablica, ktora zawiera wezly tworzace drzewo
    private $iCount;

    function __construct($sName, $sPlayer1, $sPlayer2, $aContent, $iMatchEnd, $iMatchEndV, $iWinPts) {
        $this->sRulesName = $sName;
        $this->sPlayerLabel = array($sPlayer1, $sPlayer2);
        $this->iRulesType = gameRules::$RT_TREE;
        $this->iMatchEndType = $iMatchEnd;
        $this->iMatchEndValue = $iMatchEndV;
        $this->aContent = $aContent;
        $this->iWinPoints = $iWinPts;
        $this->iCount = count($this->aContent);
    }

    function getContent() {
        return $this->aContent;
    }

    function getFirstNode() {
        return $this->aContent[0]->getId();
    }

    function getNode($iId) {
        for ($i = 0; $i < $this->iCount; $i++) {
            if ($iId == $this->aContent[$i]->getId())
                return $this->aContent[$i];
        }
    }

}

class treeNode {

    protected $iId;
    protected $sLabel;
    protected $iType;

    function getId() {
        return $this->iId;
    }

    function getLabel() {
        return $this->sLabel;
    }

    function getType() {
        return $this->iType;
    }

}

class treeNodePlayer extends treeNode {

    private $aActions; //array(array(<etykieta zachowania>,<id wezla>),array(<etykieta zachowania>,<id wezla>),...) 
    private $iGroup;

    function __construct($iId, $iType, $iGroup, $sLabel, $aActions) {
        $this->iType = $iType; //gameRules::$TN_PLAYER1 (0) lub gameRules::$TN_PLAYER2 (1)
        $this->iId = $iId;
        $this->iGroup = $iGroup;
        $this->sLabel = $sLabel;
        $this->aActions = $aActions;
    }

    function setLabel($sLabel) {
        $this->sLabel = $sLabel;
    }

    function getGroup() {
        return $this->iGroup;
    }

    function getActions() {
        return $this->aActions;
    }

}

class treeNodeNature extends treeNode {

    private $aActions; //array(array(<etykieta>,<prawdopodobienstwo>,<id wezla>))

    function __construct($iId, $aActions) {
        $this->iType = gameRules::$TN_NATURE;
        $this->iId = $iId;
        $this->sLabel = "Natura";
        $this->aActions = $aActions;
    }

    function getActions() {
        return $this->aActions;
    }

    function getRandomId() {
        $sum = 0;
        $prob = 0;
        $probs = array();

        for ($i = 0; $i < count($this->aActions); $i++) {
            $prob = $this->aActions[$i][1] * 100;
            $probs[] = $prob;
            $sum+=$prob;
        }

        $a = mt_rand(0, $sum);
        //echo "wylosowane:$a\n";

        $sum = 0;
        for ($i = 0; $i < count($probs); $i++) {
            $sum+=$probs[$i];
            //echo $sum."\n";
            if ($i == 0) {
                if ($a >= 0 && $a <= $probs[$i]) {
                    //echo "zakres: 0-".$probs[$i];
                    return $i;
                }
            } else {
                if ($a > ($sum - $probs[$i]) && $a <= $sum) {
                    //echo "zakres: ".($sum-$probs[$i])."-".$sum;
                    return $i;
                }
            }
        }
    }

}

class treeNodePoints extends treeNode {

    private $aPoints;

    function __construct($iId, $aPoints) {
        $this->iType = gameRules::$TN_POINTS;
        $this->iId = $iId;
        $this->sLabel = "Wyplata";
        $this->aPoints[0] = $aPoints[0];
        $this->aPoints[1] = $aPoints[1];
    }

    function getPlayerPoints($iIndex) {
        return $this->aPoints[$iIndex];
    }

}